package com.ms.service.module.zone.module;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import com.ms.game.jackpot.JackPotMoneyData;
import com.ms.game.jackpot.message.SBasicGameReturnJPMoneyRequest;
import com.ms.game.jackpot.message.SGameJackpotCacheResquest;
import com.ms.game.jackpot.message.SGameNotifyJackpotMoney;
import com.ms.game.lobby.GameLobbyProcesser;
import com.ms.game.lobby.message.JackpotEventType;
import com.ms.game.slot.base.message.CGmtoolWaterDisappear;
import com.ms.player.Player;
import com.ms.service.module.zone.action.BasicJackPotAction;
import com.ms.service.module.zone.db.GameJackPotDataModel;
import com.ms.service.module.zone.message.SGameJackPotLockResquest;
import com.ms.service.module.zone.message.SGameJackPotUnLockResquest;
import com.ms.service.module.zone.task.NotifyJackPotCacheTimerTask;
import com.ms.service.telnet.STelnetCmd;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.group.transportlayer.GroupMessage;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.ModuleName;
import com.xpec.c4.service.module.task.TaskTimer;

/**
 * 錢街 - 全館jp共用module
 * @author TW15012
 */
public abstract class BasicJackPotModule extends BasicServiceModule {
	
	/**
	 * S_LOBBY_JACKPOT_NOTIFY
	 * 確認是否開jp ， 有則鎖定
	 **/
	abstract public void onLockJackPot(SGameJackPotLockResquest request , BasicJackPotAction action );
	
	/**
	 * 釋放jp鎖定
	 **/
	abstract public void onUnLockJackPot(SGameJackPotUnLockResquest request , BasicJackPotAction action);
	
	/**
	 * 初始化遊戲全館JP
	 */
	abstract public void initGameJackPot(String gameType , String halltype , String hallname);
	
	/**
	 * 啟動咬幣機制
	 */
	abstract public void revWaterDisappear(CGmtoolWaterDisappear request);
	
	/**
	 * 遊戲各館彩金列表
	 */
	protected static ConcurrentHashMap<String , GameJackPotDataModel> jpList = new ConcurrentHashMap<String , GameJackPotDataModel>();
	
	/**
	 * jp brocast 快取通知時間
	 */
	public static final int CACHE_DELAY_TIMER = 5000;
	
	/**
	 * 遊戲名稱
	 */
	protected String gametype;
	
	/**
	 * 館名稱
	 */
	protected String halltype;
	
	/**
	 * 通知jp brocast 快取jp資料
	 */
	protected NotifyJackPotCacheTimerTask cachetask;
	
	
	public String getGametype() 
	{
		return gametype;
	}

	public void setGametype(String gametype) {
		this.gametype = gametype;
	}

	@Override
	public void procTelnetCommand(STelnetCmd obj) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void init() 
	{
		super.init();
		
		registerActionClass(BasicJackPotAction.class);
	}

	@Override
	public boolean startup() 
	{
		
		return super.startup();
	}
	
	@Override
	public void onModuleServerReadyStart()
	{
		super.onModuleServerReadyStart();
	}
	

	/**
	 * UpdateTaskTimer 觸發
	 * 
	 * @param periodTime
	 */
	public void onUpdateTaskTimer(long periodTime) {
		
	}

	@Override
	public void shutdown() {
		super.shutdown();
	}
	
	/**
	 * jp 快取成功回覆通知
	 */
	public void onCacheJpDataOK()
	{
		this.removeTaskTimer(cachetask);
		
		System.out.println("JP Moudle :" +  getModuleName() + " Jackpot Brocast Moudel Cache Success !!");
	}
	
	/**
	 * 廣播拉彩公告
	 */
	public void onBroadcastJackPotNotify(BigDecimal coin , BigDecimal remain , GUID pp  , String name , String hallName ,String jpname )
	{
		Player player = new Player();
		player.setName(name);
		player.setGuid(pp);
		GameLobbyProcesser.handleBroadcastJackpotNotify(this, player, JackpotEventType.IN_GAME,gametype ,hallName , jpname, coin.longValue() , remain.doubleValue());
	}
	
	/**
	 * 通知jp brocast 更新jp 金額
	 * @param data
	 */
	public void onModifyJackpotMoney(HashMap<Integer, ArrayList<JackPotMoneyData>> data)
	{
		SGameNotifyJackpotMoney request = new SGameNotifyJackpotMoney();
		request.gameName =this.gametype;
		request.halltype =this.halltype;
		request.jpdata = data;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageType(MessageTypes.S_MODIFY_JACKPOT_MONEY);
		msg.setMessageContent(request);
		msg.setDestModuleID(ModuleName.JPBORCAST);
		MessageAction.sendGroupMessage(msg);
	}
	
	
	/**
	 * 通知jp brocast  快取jp資料
	 */
	public void onNotifyJackpotCache(HashMap<Integer, ArrayList<JackPotMoneyData>> data)
	{
		SGameJackpotCacheResquest request = new SGameJackpotCacheResquest();
		request.gameName = this.gametype;
		request.halltype = this.halltype;
		request.jpdata = data;
		request.srcModuleid = getModuleName();
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageType(MessageTypes.S_GAME_CACHE_JACKPOT_MONEY);
		msg.setMessageContent(request);
		msg.setDestModuleID(ModuleName.JPBORCAST);
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 啟動通知jp brocast module 快取任務
	 */
	protected void startJpCacheTimerTask(HashMap<Integer, ArrayList<JackPotMoneyData>> list)
	{
		cachetask = new NotifyJackPotCacheTimerTask();
		cachetask.list = list;
		cachetask.module = this;
		
		
		//彩金狀態廣播
		cachetask.setTimerOption(TaskTimer.LOOP);
		cachetask.setInitialDelay(0);
		cachetask.setPeriod(CACHE_DELAY_TIMER);
		cachetask.setCallback(this);
		addTaskTimer(cachetask);
	}
	
	/**
	 * 讀取jp中彩權重值
	 * @param a
	 * @param b
	 * @param rate
	 * @param divValue
	 * @return
	 */
	public static long getJPRate(int betcount , int a , int b , long rate , long divValue)
	{
		if(divValue==0)
		{
			return rate;
		}
		
		
		long value = (((a*betcount)+b)*rate)/divValue;
		return value;
	}
	
	/**
	 * 收到遊戲交還JP錢
	 */
	public void revGameReturnJPMoney(SBasicGameReturnJPMoneyRequest request){
		
	}
	
}
